package cn.iocoder.yudao.framework.excel.core.util;


import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import java.util.List;
import java.util.Map;

/**
 * ExcelStyleTool
 *
 * @author zgd
 * @date 2024/3/13 17:16
 */
public class ExcelStyleTool {

    /**
     * 设置excel样式
     */
    public static HorizontalCellStyleStrategy getStyleStrategy() {
        // 头的策略  样式调整
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置头部样式
        setHeadStyle(headWriteCellStyle, false, true);
        // 设置细边框
        setBorder(headWriteCellStyle);
        //表头字体样式
        WriteFont headWriteFont = getHeadFont(IndexedColors.BLACK.getIndex());
        headWriteFont.setFontHeightInPoints((short)14);
        headWriteCellStyle.setWriteFont(headWriteFont);

        // 内容的策略
        WriteCellStyle contentStyle = new WriteCellStyle();
        //设置内容样式
        setHeadStyle(headWriteCellStyle, true, false);
        //设置边框
        setBorder(contentStyle);
        //内容字体
        WriteFont contentWriteFont = getContentFont();
        contentStyle.setWriteFont(contentWriteFont);
        contentStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentStyle);
    }




    /**
     * 获取表头字体
     * @param color
     * @return
     */
    private static WriteFont getHeadFont(Short color){
        //表头字体样式
        WriteFont headWriteFont = new WriteFont();
        // 头字号
        headWriteFont.setFontHeightInPoints((short) 10);
        // 字体样式
        headWriteFont.setFontName("微软雅黑");
        // 字体颜色
        headWriteFont.setColor(color);
        // 字体加粗
        headWriteFont.setBold(true);
        return headWriteFont;
    }

    /**
     * 获取内容字体
     * @return
     */
    private static WriteFont getContentFont(){
        //内容字体
        WriteFont contentWriteFont = new WriteFont();
        contentWriteFont.setFontHeightInPoints((short) 9);
        contentWriteFont.setFontName("Arial");
        contentWriteFont.setBold(false);
        return contentWriteFont;
    }

    /**
     *
     * @param cellStyle
     * @param wrappedFlag   自动换行标识，true:开启自动换行
     * @param centerFlag    水平居中开关，true:开启水平居中
     */
    private static void setHeadStyle(WriteCellStyle cellStyle, boolean wrappedFlag, boolean centerFlag){
        // 头背景 白色
        cellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        if(wrappedFlag){
            // 自动换行
            cellStyle.setWrapped(true);
        }
        if(centerFlag){
            // 水平对齐方式
            cellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        }
        // 垂直对齐方式
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    }

    /**
     * 设置边框
     * @param cellStyle
     */
    private static void setBorder(WriteCellStyle cellStyle){
        // 设置细边框
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        // 设置边框颜色 25灰度
        cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
        cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
    }

    /**
     * 得到自定义单元格策略, 内容居中
     * @return
     */
    public static BeautyStyleStrategy getBeautyCellStyleStrategyCenter(){
        //灰色表头样式
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        setHeadStyle(headWriteCellStyle, false, true);
        setBorder(headWriteCellStyle);
        WriteFont headWriteFontBlue = getHeadFont(IndexedColors.BLACK.getIndex());
        headWriteCellStyle.setWriteFont(headWriteFontBlue);
        //背景色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());

        //居中对其内容样式
        WriteFont contentWriteFont2 = getContentFont();
        WriteCellStyle contentStyleCenter = new WriteCellStyle();
        contentStyleCenter.setHorizontalAlignment(HorizontalAlignment.CENTER);
        contentStyleCenter.setVerticalAlignment(VerticalAlignment.CENTER);
//        setBorder(contentStyleCenter);
        contentStyleCenter.setWriteFont(contentWriteFont2);
        return new BeautyStyleStrategy(
                headWriteCellStyle,
                null,contentStyleCenter);
    }


    /**得到内容左对齐的策略
     * @return
     */
    public static BeautyStyleStrategy getBeautyCellStyleStrategyLeft(){
        //灰色表头样式
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        setHeadStyle(headWriteCellStyle, true, true);
        setBorder(headWriteCellStyle);
        WriteFont headWriteFont = getHeadFont(IndexedColors.BLACK.getIndex());
        headWriteCellStyle.setWriteFont(headWriteFont);
        //背景色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());

        //内容文字
        WriteFont contentWriteFont = getContentFont();
        //左对齐内容样式
        WriteCellStyle contentStyleLeft = new WriteCellStyle();
        contentStyleLeft.setHorizontalAlignment(HorizontalAlignment.LEFT);
        contentStyleLeft.setVerticalAlignment(VerticalAlignment.CENTER);
//        setBorder(contentStyleLeft);
        contentStyleLeft.setWriteFont(contentWriteFont);
        return new BeautyStyleStrategy(
                headWriteCellStyle,
                contentStyleLeft,null);
    }

    public static CustomColumnWidthStyleStrategy getColumnWidthStrategy(int minBytes, int maxBytes){
        return new CustomColumnWidthStyleStrategy(minBytes,maxBytes);
    }
    public static CustomColumnWidthStyleStrategy getAutoBeautyColumnWidthStrategy(){
        //比较合适的自适应宽度
        return new CustomColumnWidthStyleStrategy(8,50);
    }

//    /**
//     * @param headIdx  标题行
//     * @param colIdx   错误所在列. 下标从0开始. 如果没指定,自动取标题行的下一列
//     * @param errTitle
//     * @param errMap   错误信息,key是内容list的下标(为了方便list遍历时传值),最终它的行是 headIdx+errMap+1
//     */
//    public static AddErrColWriteHandler getAddErrColWriteHandler(Integer headIdx, Integer colIdx, String errTitle, Map<Integer, String> errMap){
//        return new AddErrColWriteHandler(headIdx, colIdx, errTitle, errMap);
//    }

//    /**
//     *  @param headIdx  标题行
//     *   @param errTitle
//     *    @param errMap   错误信息,key是内容list的下标,从0开始(为了方便list遍历时传值),最终它的行是 headIdx+errMap+1
//     */
//    public static AddErrColWriteHandler getAddErrColWriteHandler( Integer headIdx, String errTitle, Map<Integer, String> errMap){
//        return new AddErrColWriteHandler(headIdx,  errTitle, errMap);
//    }

//    /**
//     * 获取合并单元格处理器
//     * @return
//     */
//    public static CustomMergeCellWriteHandler getMergeHandler() {
//        return new CustomMergeCellWriteHandler();
//    }
//
//
//    /**
//     * 获取合并单元格处理器
//     * @param firstRow
//     * @param lastRow
//     * @param firstCol
//     * @param lastCol
//     * @return
//     */
//    public static CustomMergeCellWriteHandler getMergeHandler(int firstRow, int lastRow, int firstCol, int lastCol) {
//        return new CustomMergeCellWriteHandler(firstRow, lastRow, firstCol, lastCol);
//    }
//
//
//    /**
//     * 获取 按列 相同的值进行合并的处理器
//     * @param colList
//     * @return
//     */
//    public static LoopColRangeWriteHandler getLoopColRangeWriteHandler(List<Integer> colList,int fromRow,int toRow) {
//        return new LoopColRangeWriteHandler(colList,fromRow,toRow);
//    }
}


